package projet.volleystat;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import projet.volleystat.bdd.VolleyRepository;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PlacePlayersActivity extends Activity {
	protected int myteamid;
	protected int advteamid;
	protected Team myteam;
	protected Team advteam;
	protected EditText myteamtext;
	protected EditText advteamtext;
	protected Button validateb;
	protected VolleyRepository volleyDataBase;
	protected TreeMap<Integer, Player> myavailableplayersbyid;
	protected TreeMap<Integer, Player> advavailableplayersbyid;
	protected ArrayList<Player> allmyplayers;
	protected ArrayList<Player> alladvplayers;
	protected SparseArray<Button> myplayersb;
	protected SparseArray<Button> advplayersb;
	protected SparseArray<Player> myplayers;
	protected SparseArray<Player> advplayers;
	protected SparseArray<Player> myreplacementplayers;
	protected SparseArray<Player> advreplacementplayers;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_place_players);

		int[] myplayersbid = new int[] { R.id.placemyteamplayerb1,
				R.id.placemyteamplayerb2, R.id.placemyteamplayerb3,
				R.id.placemyteamplayerb4, R.id.placemyteamplayerb5,
				R.id.placemyteamplayerb6 };

		int[] advplayersbid = new int[] { R.id.placeadvteamplayerb1,
				R.id.placeadvteamplayerb2, R.id.placeadvteamplayerb3,
				R.id.placeadvteamplayerb4, R.id.placeadvteamplayerb5,
				R.id.placeadvteamplayerb6 };

		myplayersb = new SparseArray<Button>();
		advplayersb = new SparseArray<Button>();

		for (int i = 1; i <= 6; i++) {
			myplayersb.append(i, (Button) findViewById(myplayersbid[i - 1]));
			advplayersb.append(i, (Button) findViewById(advplayersbid[i - 1]));
		}

		myteamtext = (EditText) findViewById(R.id.placemyteamtext);
		advteamtext = (EditText) findViewById(R.id.placeadvteamtext);
		validateb = (Button) findViewById(R.id.validateplacebutton);

		myplayers = new SparseArray<Player>();
		advplayers = new SparseArray<Player>();

		Bundle bundle = getIntent().getExtras();
		myteamid = bundle.getInt("myteamid");
		advteamid = bundle.getInt("advteamid");

		volleyDataBase = new VolleyRepository(getApplicationContext());
		volleyDataBase.open();

		allmyplayers = new ArrayList<Player>();
		alladvplayers = new ArrayList<Player>();

		try {
			myteamtext.setText(volleyDataBase.getTeamById(myteamid).getName());
			advteamtext
			.setText(volleyDataBase.getTeamById(advteamid).getName());
			myteam = volleyDataBase.getTeamById(myteamid);
			advteam = volleyDataBase.getTeamById(advteamid);
			allmyplayers = volleyDataBase.getAllPlayerInTeam(myteam);
			alladvplayers = volleyDataBase.getAllPlayerInTeam(advteam);
		} catch (DatabaseException e) {
			e.printStackTrace();
		}

		volleyDataBase.close();

		validateb.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MatchActivity.class);

				int[] myplayersarray = new int[myplayers.size() + 1];
				int[] advplayersarray = new int[advplayers.size() + 1];
				ArrayList<Integer> myreplacementplayersarray = new ArrayList<Integer>();
				ArrayList<Integer> advreplacementplayersarray = new ArrayList<Integer>();

				for (int i = 1; i <= 6; i++) {
					try {
						myplayersarray[i] = myplayers.get(i).getId();
					} catch (NullPointerException e) {
						Toast.makeText(getBaseContext(),
								"Merci de placer les joueurs",
								Toast.LENGTH_SHORT).show();
						return;
					}

				}

				for (int i = 1; i <= 6; i++) {

					try {
						advplayersarray[i] = advplayers.get(i).getId();
					} catch (NullPointerException e) {
						Toast.makeText(getBaseContext(),
								"Merci de placer les joueurs",
								Toast.LENGTH_SHORT).show();
						return;
					}
				}

				for (Map.Entry<Integer, Player> entry : myavailableplayersbyid
						.entrySet()) {
					myreplacementplayersarray.add(entry.getValue().getId());
				}

				for (Map.Entry<Integer, Player> entry : advavailableplayersbyid
						.entrySet()) {
					advreplacementplayersarray.add(entry.getValue().getId());
				}
				
				  intent.putExtra("myplayersarray", myplayersarray);
				  intent.putExtra("advplayersarray", advplayersarray);
				  intent.putExtra("myreplacementplayersarray",
				  myreplacementplayersarray);
				  intent.putExtra("advreplacementplayersarray",
				  advreplacementplayersarray); intent.putExtra("myteamid",
				  myteamid); intent.putExtra("advteamid", advteamid);
				  
				  intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION
				  );
				  
				  startActivity(intent);
				 
			}
		});

		myavailableplayersbyid = new TreeMap<Integer, Player>();
		advavailableplayersbyid = new TreeMap<Integer, Player>();

		for (int i = 0; i < allmyplayers.size(); i++)
			myavailableplayersbyid.put(allmyplayers.get(i).getId(),
					allmyplayers.get(i));

		for (int i = 0; i < alladvplayers.size(); i++)
			advavailableplayersbyid.put(alladvplayers.get(i).getId(),
					alladvplayers.get(i));

		for (int i = 1; i <= 6; i++) {
			final int idbuttonmyteam = i;
			myplayersb.get(i).setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getApplicationContext(),
							SelectPlayerActivity.class);

					ArrayList<Integer> playersarray = new ArrayList<Integer>();

					for (Map.Entry<Integer, Player> entry : myavailableplayersbyid
							.entrySet()) {
						playersarray.add(entry.getKey());
					}

					intent.putExtra("playersarray", playersarray);
					intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivityForResult(intent, 10 + idbuttonmyteam);
				}
			});
		}

		for (int i = 1; i <= 6; i++) {
			final int idbuttonadvteam = i;
			advplayersb.get(i).setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(getApplicationContext(),
							SelectPlayerActivity.class);

					ArrayList<Integer> playersarray = new ArrayList<Integer>();

					for (Map.Entry<Integer, Player> entry : advavailableplayersbyid
							.entrySet()) {
						playersarray.add(entry.getKey());
					}

					intent.putExtra("playersarray", playersarray);
					intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION);
					startActivityForResult(intent, 20 + idbuttonadvteam);
				}
			});
		}
	}

	public void deleteSelectablePlayer(int id) {
		if (myavailableplayersbyid.containsKey(id))
			myavailableplayersbyid.remove(id);
		else if (advavailableplayersbyid.containsKey(id))
			advavailableplayersbyid.remove(id);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		for (int i = 11; i <= 16; i++) {
			if (requestCode == i) {
				if (resultCode == Activity.RESULT_OK) {
					volleyDataBase.open();
					try {
						if (myplayers.get(i - 10) != null) {
							myavailableplayersbyid.put(myplayers.get(i - 10)
									.getId(), myplayers.get(i - 10));
						}
						myplayers.put(i - 10, volleyDataBase.getPlayerById(data
								.getIntExtra("selectedplayerid", -1)));
					} catch (DatabaseException e1) {
						e1.printStackTrace();
					}
					myplayersb.get(i - 10).setText(
							myplayers.get(i - 10).getName());
					deleteSelectablePlayer(myplayers.get(i - 10).getId());
					volleyDataBase.close();
					break;
				}
			}
		}

		for (int i = 21; i <= 26; i++) {
			if (requestCode == i) {
				if (resultCode == Activity.RESULT_OK) {
					volleyDataBase.open();
					try {
						if (advplayers.get(i - 20) != null) {
							advavailableplayersbyid.put(advplayers.get(i - 20)
									.getId(), advplayers.get(i - 20));
						}
						advplayers.put(i - 20, volleyDataBase
								.getPlayerById(data.getIntExtra(
										"selectedplayerid", -1)));
					} catch (DatabaseException e1) {
						e1.printStackTrace();
					}
					advplayersb.get(i - 20).setText(
							advplayers.get(i - 20).getName());
					deleteSelectablePlayer(advplayers.get(i - 20).getId());
					volleyDataBase.close();
					break;
				}
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		volleyDataBase.open();
	}

	@Override
	public void onPause() {
		super.onPause();
		volleyDataBase.close();
		overridePendingTransition(0, 0);
	}
}
